<?
// Prado::using('System.Web.DateUtils');
class SearchUserRenderer extends TRepeaterItemRenderer
{
	
	public function onInit($param)
    {
//		echo "ss ". $this->getPage()->friend;
		$this->filteringButton();
		$this->highlightMatchPatterns();
	}
	
	public function highlightMatchPatterns()
	{
		$pattern = $this->getPage()->search_str;
		// wrap the match string with the span with class .. with non-case sensitive(str_ireplace)
		$html = str_ireplace($pattern,"<span class=\"textHilight\">".$pattern ."</span>" ,$this->Data->username);
		//die($html);
		$this->txtUsername->Text = $html;
	}
	public function setLoggedInSetting()
	{///// Special cases
//		echo $this->Data->user_id ."," . $this->user->ID;
		if($this->Data->user_id == $this->user->ID)
		{ // found record match himself... 
			$this->btnAddFriend->Visible = false;
			$this->btnRemoveFriend->Visible = false;
			$this->btnSendMSG->Visible = false;
		}
		
		if(in_array($this->Data->user_id,$this->getPage()->friend) )
		{ // they're friends
			$this->btnAddFriend->Visible = false;
			$this->btnRemoveFriend->Visible = false;
		}
		else // they're not friends yet
		{
			$this->verifyPendingRequest();
			$this->btnRemoveFriend->Visible = false;
		}
	}
	
	public function verifyPendingRequest()
	{
		$count = FriendReqRecord::finder()->count('user_id = ? AND friend_id = ?',$this->user->ID ,$this->Data->user_id);
		
		//echo $this->user->ID . ",".$this->Data->user_id . "," . $count . "<br/>";
//		die();
		if($count > 0)
		{// they already sent a request
			$this->btnAddFriend->Visible = false;
			$this->txtPending->Text = "Requested Maded.. ";
		}
	}
	
	public function buttonRequestedClicked($sender, $param)
    {
		$f_email = UserRecord::finder()->findByPk($this->HIDDEN_ID->Value);
//        $f_email = UserRecord::finder()->findByPk($this->Data->user_id);
//		echo "insertRecord ".$this->User->ID." , ".$this->Data->user_id;
		$this->insertRecord($f_email);	
		$this->getPage()->onInit($param);
//		$url = $this->Service->constructUrl('users.ProfileUser',array('id' => $this->user->ID));
//        $this->Response->redirect($url);
		//$this->onInit($param); // call this onInit to refresh the page
		
    }
	
	public function insertRecord($finder)
	{
		$f_id = $finder->user_id;
		//$owner = UserRecord::finder()->findByEmail($this->user->Email);
		
		$u_id = $this->User->ID;
		
//		die();
		$newRec = new FriendReqRecord();
		$newRec->user_id = $u_id;
		$newRec->friend_id = $f_id;
		$newRec->datetime_request = date("Y/m/d H:i:s");
		$newRec->save();
	}
	
	public function setNotLoggedInSetting()
	{
		$this->btnAddFriend->Visible = false;
		$this->btnRemoveFriend->Visible = false;
		$this->btnSendMSG->Visible = false;
	}
	
	public function filteringButton()
	{
		//echo "<br/>size: ".($this->getPage()->friend == null)."         ";
//		if(!isset($this->getPage()->friend))
		if($this->getPage()->friend === null)
		{
//			echo "null";
			$this->setNotLoggedInSetting();
		}
		else
		{	
//			echo "not null";
			$this->setLoggedInSetting();
		}
		
	}

}

?>